<template>
	<view class="container" style="padding-bottom: 120rpx;">
		<u--form ref="queryForm" :model="query">
			<u-form-item label="" prop="containerCode" style="display: flex;">
				<span style="margin-right: 20rpx;">物料码</span>
				<u--input
					placeholder="请输入/扫描"
					v-model="query.materialCode"
					suffixIcon="scan"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="materialBatch" style="display: flex;">
				<span style="margin-right: 20rpx;">批次号</span>
				<u--input
					placeholder="请输入/扫描"
					v-model="query.materialBatch"
					suffixIcon="scan"
					suffixIconStyle="font-size: 68rpx"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
			<u-form-item label="" prop="quantity" style="display: flex;">
				<span style="margin-right: 50rpx;">数量</span>
				<u--input
					type="number"
					placeholder="请输入/扫描"
					v-model="query.quantity"
					style="flex: 1;"
				>
				</u--input>
			</u-form-item>
		</u--form>
		<view class="buttonStyle">
			<u-button type="primary" text="确认绑定" @click="submit"></u-button>
			<u-button type="warning" text="重置" @click="reset"></u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import { vmInventoryAPI } from '@/api/work/inbound'
	export default {
		data() {
			return {
				query: {
					materialCode: '',
					materialBatch: '',
					quantity: ''
				}
			};
		},
		onLoad(options) {
			uni.$on('scancodedate',(data) => this.getCode(data.code))
		},
		onUnload() {  
		  uni.$off('scancodedate')
		},
		methods: {
			getCode(code) {
				if (code.length === 18) {
					this.query.materialCode = code
				} else if (code.length > 0) {
					this.query.materialBatch = code
				}
			},
			submit() {
				if (!this.query.materialCode) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描物料号'
					})
				}
				if (this.query.materialCode && !this.query.materialBatch) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描批次号'
					})
				}
				if (this.query.materialCode && this.query.materialBatch && !this.query.quantity) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请输入数量'
					})
				}
				if (!/^\d+$/.test(this.query.quantity)) {
					return this.$refs.uToast.show({
						type: 'warning',
						message: '数量须大于等于0'
					})
				}
				this.bind()
			},
			reset() {
				this.query = {
					materialCode: '',
					materialBatch: '',
					quantity: ''
				}
			},
			async bind() {
				const res = await vmInventoryAPI(this.query)
				if (res.meta.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: res.meta.msg
					})
					this.reset()
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.meta.msg
					})
				}
			}
		}
	}
</script>

<style lang="scss">
.buttonStyle {
	display: flex;
	gap: 20rpx;
	box-sizing: border-box;
	position: fixed;
	left: 0;bottom: 0;
	padding: 10rpx;
	width: 100%;
	background-color: #fff;
}
</style>
